#include "gp_seal_ctl.h"
#include "sock_msg_ctl.h"
#include "gp_layout_map.h"
#include "stdio.h"
#include "string.h"
#include "../lua/action_msg.h"
#include "resource_manage.h"
#include <unistd.h>
#include "../data_proc/board_data_proc.h"


void sealActFunc(void * paramIn,void * actResultOut)
{
    if(!paramIn || !actResultOut)
    {
        return;
    }
    printf("enter in sealActFunc\n");
    act_msg_node * pstActMsg = (act_msg_node *)paramIn;
    int actSuccess = 0;
    
    if(pstActMsg->msgHead.actType == ACT_SINGLE_MOVE)
    {
        if(pstActMsg->msgHead.paramType == PARAM_AXIS_MOVE_STEPS)
        {
            int component = -1;
            int steps = 0;
            char * pData = (char*)pstActMsg->paramData;

            memcpy(&component,pData,sizeof(int));
            pData += sizeof(int);

            memcpy(&steps,pData,sizeof(int));
            actSuccess = seal_single_move_steps(&steps,component);

        }
        else if(pstActMsg->msgHead.paramType == PARAM_AXIS_MOVE_POS)
        {
            location_layout locLayout;
            location_axis locAxis;
            memcpy(&locLayout,((char*)pstActMsg->paramData) + sizeof(int),sizeof(location_layout));
            int component = -1;
            memcpy(&component,(char*)pstActMsg->paramData,sizeof(int));
            actSuccess = seal_single_move_pos(locLayout.location_type,component);
        }
    }
    else if(pstActMsg->msgHead.actType == ACT_SINGLE_INIT)
    {
        int component = -1;
        memcpy(&component,(char*)pstActMsg->paramData,sizeof(int));
        printf("sealActFunc,single init,componet:%d\n",component);
        actSuccess = seal_single_init(NULL,component);
    }
    else if(ACT_CHAIN_ACTION == pstActMsg->msgHead.actType)
    {
        if(pstActMsg->msgHead.paramType == PARAM_SEAL_CTL)
        {
            int downSteps = -1;

            memcpy(&downSteps,pstActMsg->paramData,sizeof(int));          
            actSuccess = seal_heatseal(downSteps);
        }
        
    }
    else if(ACT_MODULE_INIT == pstActMsg->msgHead.actType)
    {
        actSuccess = seal_module_init();
    }
    else if(ACT_HEAT_CTL == pstActMsg->msgHead.actType)
    {

        int chanNum = pstActMsg->msgHead.paramType;
        int isStart = 0;
        char * pData = (char *)pstActMsg->paramData;

        memcpy(&isStart,pData,sizeof(int));

        heatParam heatCtlParam;
        if(isStart)
        {
            heatCtlParam.opcode = CENTRE_START;
        }
        else
        {
            heatCtlParam.opcode = CENTRE_STOP;
        }

        pData += sizeof(int);
        memcpy(&heatCtlParam.temperature,pData,sizeof(int));

        printf("seal, ACT_HEAT_CTL,temprature:%f\n",heatCtlParam.temperature);

        heatCtlParam.chan = chanNum; 


        actSuccess = sealCtl(&heatCtlParam,HEAT_CTL,5000);        
    }
    else if(pstActMsg->msgHead.actType == ACT_TEMPERATURE_READ)
    {
        printf("param read,seal\n");
        temp_read_param tempReadParam;
        memset(&tempReadParam,0,sizeof(temp_read_param));
        int boardId = THERMAL_SEAL_BOARD;
        int opcode = PURIFY_TEMPERATURE;
        
        tempReadParam.chanNum = pstActMsg->msgHead.paramType;
        memcpy(&tempReadParam.isStart,pstActMsg->paramData,sizeof(int));
        

        memcpy(&tempReadParam.interv,(char*)(pstActMsg->paramData) + sizeof(int),sizeof(int));
        
        printf("interv:%d\n",tempReadParam.interv);

        actSuccess = readTempCtl(&tempReadParam,boardId,opcode, 2000);
     
    }

    msgAckSock * pstResult = (msgAckSock *)actResultOut;

    if(!actSuccess)
    {
        pstResult->status = 1;
    }
    else
    {
        pstResult->status = 0;
    }
}


int convertToSealSid(int componentIn)
{
    int sid = -1;
    switch (componentIn)
    {
        case MT_AXIS_Z:
        {
            sid = SID_SEAL_Z;
        }
        break;
        case MT_AXIS_Y:
        {
            sid = SID_SEAL_Y;
        }
        break;
        default:
        break;
    }
    return sid; 
}

int seal_single_init(void * paramIn,int component)
{
    int sid = convertToSealSid(component);
    int actSuccess = 0;
    if(sid==-1)
    {
        return actSuccess;
    }

    move_param moveParam;

    moveParam.sid = sid;
    moveParam.pos = 0;

    moveParam.dec_speed = 80000;
    moveParam.inc_speed = 80000;
    moveParam.speed = 20000;        

    
    actSuccess = uimInitAct_multi(&moveParam, 1, 20000);

    return actSuccess;
}


int seal_single_move_steps(void * paramIn,int component)
{
    int sid = convertToSealSid(component);
    int actSuccess = 0;
    if(sid==-1)
    {
        return actSuccess;
    }

    move_param moveParam;

    int * pSteps = (int *)paramIn; 

    printf("in seal_single_move_steps, steps:%d\n",* pSteps);

    moveParam.sid = sid;
    moveParam.pos = * pSteps;

    moveParam.dec_speed = 40000;
    moveParam.inc_speed = 40000;
    moveParam.speed = 10000;        

    
    actSuccess = uimMoveAct_multi(&moveParam, 1, 20000);

    return actSuccess;   
}

int seal_heatseal(int downStepsIn)
{
    CGpLayout * pstLayout = CGpLayout::get_instance();
    locs_seal * pstSealPos = NULL;//pstLayout->getSealPos();
    move_param moveParamArr[2];
    memset(moveParamArr,0,sizeof(move_param)*2);
    int actSuccess = 0;

    if(pstLayout==NULL)
    {
        return actSuccess;
    }

    pstSealPos = pstLayout->getSealPos();
    if(pstSealPos==NULL)
    {
       return actSuccess; 
    }
    //1,check the film,

    //2, y go to work pos
    moveParamArr[0].pos = pstSealPos->x_work;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;    
    moveParamArr[0].dec_speed = 80000;
    moveParamArr[0].sid = SID_SEAL_Y;

    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("seal_heatseal, y goto work pos failed\n");
        return actSuccess;
    }

    //3, z go to work pos
    if(downStepsIn==-1)
    {
        moveParamArr[0].pos = pstSealPos->z_work;  
    }
    else
    {
        moveParamArr[0].pos = downStepsIn; 
    }
    
    moveParamArr[0].pos = pstSealPos->z_work;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;    
    moveParamArr[0].dec_speed = 80000;
    moveParamArr[0].sid = SID_SEAL_Z;

    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("seal_heatseal, z goto work pos failed\n");
        return actSuccess;
    }

    //4, delay
    usleep(1000*SEAL_HEAT_DURATION);

    //5, z  go up
    moveParamArr[0].pos = 0;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;    
    moveParamArr[0].dec_speed = 80000;
    moveParamArr[0].sid = SID_SEAL_Z;

    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("seal_heatseal, z go up failed\n");
        return actSuccess;
    }

    //6, y go out
    moveParamArr[0].pos = pstSealPos->x_wait;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;    
    moveParamArr[0].dec_speed = 80000;
    moveParamArr[0].sid = SID_SEAL_Y;

    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("seal_heatseal, y go out failed\n");
    }

    return actSuccess;
}

int seal_module_init(void)
{
    move_param moveParam;
    memset(&moveParam,0,sizeof(move_param));
    int actSuccess = 0;
    moveParam.sid = SID_SEAL_Z;//SID_DP8_Z;
    moveParam.pos = 0;
    moveParam.dec_speed = 40000;
    moveParam.inc_speed = 40000;
    moveParam.speed = 10000;

    actSuccess = uimInitAct_multi(&moveParam, 1, 20000);

    if(actSuccess != 1)
    {
        printf("seal_module_init,seal z go 0 failed,actRet:%d\n",actSuccess);
        return actSuccess;
    }

    moveParam.sid = SID_SEAL_Y;
    moveParam.pos = 0;
    moveParam.dec_speed = 40000;
    moveParam.inc_speed = 40000;
    moveParam.speed = 10000;

    actSuccess = uimInitAct_multi(&moveParam, 1, 20000);

    if(actSuccess != 1)
    {
        printf("seal_module_init,seal y go 0 failed,actRet:%d\n",actSuccess);
        return actSuccess;
    }

    CGpLayout * pstLayout = CGpLayout::get_instance();
    locs_seal * pstSealPos = NULL;

    if(pstLayout == NULL)
    {
        printf("seal_module_init,get location handle failed\n");
        return 0;
    }

    pstSealPos = pstLayout->getSealPos();

    if(pstSealPos == NULL)
    {
        printf("seal_module_init, get seal location failed\n");
        return 0;       
    }

    moveParam.sid = SID_SEAL_Y;
    moveParam.pos = pstSealPos->x_wait;
    moveParam.dec_speed = 32000;
    moveParam.inc_speed = 32000;
    moveParam.speed = 8000;

    actSuccess = uimMoveAct_multi(&moveParam, 1, 20000);

    if(actSuccess != 1)
    {
        printf("seal_module_init,seal y go to wait failed,actRet:%d\n",actSuccess);        
    }

    return actSuccess;
}

int seal_single_move_pos(int posTypeIn, int component)
{
    CGpLayout * pstLayout = CGpLayout::get_instance();
    locs_seal * pstSealPos = NULL;

    if(pstLayout == NULL)
    {
        printf("seal_single_move_pos,get location handle failed\n");
        return 0;
    }

    pstSealPos = pstLayout->getSealPos();

    if(pstSealPos == NULL)
    {
        printf("seal_single_move_pos, get seal location failed\n");
        return 0;       
    }

    int posToMove = -1;
    int sid = SID_SEAL_Y;

    if(component == MT_AXIS_Z)
    {
        if(posTypeIn == SEAL_Z_WORK)
        {
            posToMove = pstSealPos->z_work;
        }
        else if(posTypeIn == SEAL_Z_WAIT)
        {
            posToMove = pstSealPos->z_wait;
        }

        sid = SID_SEAL_Z;        
    }
    else if(component == MT_AXIS_Y || component == MT_AXIS_X)
    {
        if(posTypeIn == SEAL_X_WAIT)
        {
            posToMove = pstSealPos->x_wait;
        }
        else if(posTypeIn == SEAL_X_WORK)
        {
            posToMove = pstSealPos->x_work;
        } 

        sid = SID_SEAL_Y;          
    }

    if(posToMove < 0)
    {
        printf("seal_single_move_pos, calc steps failed,%d\n",posToMove);
        return 0;
    }

    move_param moveParam;
    memset(&moveParam,0,sizeof(move_param));

    moveParam.sid = sid;
    moveParam.pos = posToMove;
    moveParam.speed = 10000;
    moveParam.inc_speed = 40000;
    moveParam.dec_speed = 40000;


    return uimMoveAct_multi(&moveParam,1,20000);

}